﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class Parser
    {
        PlatformerGame game;
        Random rand = new Random();

        public Parser(PlatformerGame game)
        {
            this.game = game;
        }

        public void CreateMap(int level)
        {
            StreamWriter writer = new StreamWriter(@"C:\Users\YVAN\Desktop\ee.txt");
            string line = null;
            StreamReader reader = new StreamReader("Components/Map/level" + level);
            int Y = GetHeight("Components/Map/level" + level);

            for (int y = 0; y < Y; y++)
            {
                line = reader.ReadLine();
                if (line == null)
                    break;
                for (int x = 0; x < line.Length; x++)
                {
                    if (line[x] == 'x')
                    {
                        writer.WriteLine("x :"+x);
                        writer.WriteLine("y :"+y);
                        writer.WriteLine("bloc size :"+Block.Size);
                        Block temp = new Block(game.Content, new Vector2(x * Block.Size.X, y * Block.Size.Y), rand.Next(0, 7));
                        writer.WriteLine("temp.X :"+temp.Position.X);
                        writer.WriteLine("temp.Y :"+temp.Position.Y);
                        game.BlocksList.Add(temp);
                    }
                }
            }
            writer.Close();
            reader.Close();
        }

        int GetHeight(string file)
        {
            int total = 0;
            string line = null;
            StreamReader reader = new StreamReader(file);

            do
            {
                line = reader.ReadLine();
                total++;
            } while (line != null);

            reader.Close();

            return total;
        }
    }
}
